package cn.songtx.iot.service.impl;

import cn.songtx.iot.mapper.SysFamilyMapper;
import cn.songtx.iot.mapper.SysUserFamilyMapper;
import cn.songtx.iot.mapper.SysUserMapper;
import cn.songtx.iot.pojo.SysFamily;
import cn.songtx.iot.pojo.SysUser;
import cn.songtx.iot.pojo.SysUserFamily;
import cn.songtx.iot.service.ISysUserFamilyService;
import cn.songtx.iot.vo.UserFamilyVO;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@DS("master")
public class SysUserFamilyServiceImpl extends ServiceImpl<SysUserFamilyMapper, SysUserFamily> implements ISysUserFamilyService {

    @Autowired
    SysFamilyMapper sysFamilyMapper;
    @Autowired
    SysUserMapper sysUserMapper;

    /**
     * 家庭关系:查询具有管理权限的家庭关系
     * 1.根据sysUserId查询sysFamily表,返回familyIds
     * 2.根据familyIds查询sysUserFamily表,返回家庭关系
     *
     * @param sysUserId 用户id
     * @return 返回可管理的家庭关系
     */
    @Override
    public List<UserFamilyVO> selectUserFamilyListByOwner(Long sysUserId) {
        // 1. 根据sysUserId查询sys_family表,返回list,获得id和familyName
        LambdaQueryWrapper<SysFamily> sysFamilyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysFamilyLambdaQueryWrapper.select(SysFamily::getId, SysFamily::getFamilyName)
                .eq(SysFamily::getSysUserId, sysUserId);
        List<SysFamily> sysFamilies = sysFamilyMapper.selectList(sysFamilyLambdaQueryWrapper);

        // 2. 无条件查询sys_user表,返回list,获得id和userName
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.select(SysUser::getId, SysUser::getUserName);
        List<SysUser> sysUsers = sysUserMapper.selectList(sysUserLambdaQueryWrapper);
        // 2.1 无条件查询sys_family表,返回list,获得id和familyName
        sysFamilyLambdaQueryWrapper.select(SysFamily::getId, SysFamily::getFamilyName);
        List<SysFamily> sysFamiliesList = sysFamilyMapper.selectList(sysFamilyLambdaQueryWrapper);

        // 3. 从返回的sysFamilies对象数组中取ids,stream的map操作
        List<Long> familyIds = sysFamilies.stream().map(SysFamily::getId).toList();
        // 4. 根据familyIds查询sysUserFamily表,返回家庭关系
        LambdaQueryWrapper<SysUserFamily> sysUserFamilyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserFamilyLambdaQueryWrapper.select(SysUserFamily::getId, SysUserFamily::getUserId, SysUserFamily::getFamilyId)
                .in(SysUserFamily::getFamilyId, familyIds);
        List<SysUserFamily> sysUserFamilies = this.list(sysUserFamilyLambdaQueryWrapper);

        // 5. PO->VO,增强for赋值
        List<UserFamilyVO> userFamilyVOS = new ArrayList<>();
        for (SysUserFamily sysUserFamily : sysUserFamilies) {
            UserFamilyVO userFamilyVO = new UserFamilyVO();
            BeanUtils.copyProperties(sysUserFamily, userFamilyVO);
            userFamilyVOS.add(userFamilyVO);
        }

        // 6. 连表赋值解析,userId->userName,familyId->familyName
        Map<Long, String> familyNameMap = sysFamiliesList.stream().collect(Collectors.toMap(SysFamily::getId, SysFamily::getFamilyName));
        Map<Long, String> userNameMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getId, SysUser::getUserName));
        for (UserFamilyVO userFamilyVO : userFamilyVOS) {
            userFamilyVO.setFamilyName(familyNameMap.get(userFamilyVO.getFamilyId()));
            userFamilyVO.setUserName(userNameMap.get(userFamilyVO.getUserId()));
        }
        return userFamilyVOS;
    }

    /**
     * 家庭关系:所有账户可查,查询本账户拥有的家庭列表,单表查询,连表解析
     *
     * @param sysUserId 用户id
     * @return 返回可查询的家庭关系
     */
    @Override
    public List<UserFamilyVO> selectUserFamilyListByUser(Long sysUserId) {
        // 1. 无条件查询sys_family表,返回list,获得id和familyName
        LambdaQueryWrapper<SysFamily> sysFamilyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysFamilyLambdaQueryWrapper.select(SysFamily::getId, SysFamily::getFamilyName);
        List<SysFamily> sysFamilies = sysFamilyMapper.selectList(sysFamilyLambdaQueryWrapper);
        // 2. 无条件查询sys_user表,返回list,获得id和userName
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.select(SysUser::getId, SysUser::getUserName);
        List<SysUser> sysUsers = sysUserMapper.selectList(sysUserLambdaQueryWrapper);
        // 3.根据sysUserId查询sys_user_family表,返回list
        LambdaQueryWrapper<SysUserFamily> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserFamily::getUserId, sysUserId);
        List<SysUserFamily> sysUserFamilies = this.list(lambdaQueryWrapper);
        // 4.PO-VO,数据组装
        List<UserFamilyVO> userFamilyVOS = new ArrayList<>();
        for (SysUserFamily sysUserFamily : sysUserFamilies) {
            UserFamilyVO userFamilyVO = new UserFamilyVO();
            BeanUtils.copyProperties(sysUserFamily, userFamilyVO);
            userFamilyVOS.add(userFamilyVO);
        }
        // 5. 连表赋值解析,userId->userName,familyId->familyName
        Map<Long, String> familyNameMap = sysFamilies.stream().collect(Collectors.toMap(SysFamily::getId, SysFamily::getFamilyName));
        Map<Long, String> userNameMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getId, SysUser::getUserName));
        for (UserFamilyVO userFamilyVO : userFamilyVOS) {
            userFamilyVO.setFamilyName(familyNameMap.get(userFamilyVO.getFamilyId()));
            userFamilyVO.setUserName(userNameMap.get(userFamilyVO.getUserId()));
        }
        return userFamilyVOS;
    }
}
